package com.huishu.coupon.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.pojo.dto.FindAvailableCouponDTO;
import com.huishu.commons.pojo.dto.SendRepurchaseDTO;
import com.huishu.commons.pojo.vo.CouponVO;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.coupon.pojo.dto.CouponCreateDTO;
import com.huishu.coupon.pojo.dto.CouponPageDTO;
import com.huishu.coupon.pojo.dto.CouponUpdateDTO;
import com.huishu.coupon.pojo.model.CouponDO;
import com.huishu.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author generator@ljj
 * @since 2023-04-10
 */
@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponController {

    @Autowired
    private CouponService couponService;

   /* @GetMapping("/findCouponByProductId/{productId}")
    public Result<List<CouponVO>> findCouponByProductId(@PathVariable Long productId) {
        log.info("根据商品id查询优惠卷");
        return couponService.findCouponByProductId(productId);
    }

    @GetMapping("/findCouponByProductCategoryId/{productCategoryId}")
    public Result<List<CouponVO>> findCouponByProductCategoryId(@PathVariable Long productCategoryId) {
        log.info("根据品类查询优惠卷");
        return couponService.findCouponByProductCategoryId(productCategoryId);
    }*/

    @PostMapping("/find-available-coupon")
    public Result<List<CouponVO>> findAvailableCoupon(@RequestBody FindAvailableCouponDTO findAvailableCouponDTO) {
        return couponService.findAvailableCoupon(findAvailableCouponDTO);
    }

    @PostMapping("/find-coupon-byUserId/{userId}")
    public Result<List<CouponVO>> findCouponByUserId(@PathVariable Long userId, @RequestBody List<Long> categoryId) {
        return couponService.findCouponByUserId(userId,  categoryId);
    }

    @PostMapping("/find-repurchase-byUserId/{userId}")
    public Result<List<CouponVO>> findRepurchaseByUserId(@PathVariable Long userId, @RequestBody List<Long> categoryId) {
        return couponService.findRepurchaseByUserId(userId, categoryId);
    }

    @GetMapping("/find-unclaimed")
    public Result findUnclaimed() {
        return couponService.findUnclaimed();
    }

    @GetMapping("/find-repurchase-coupon")
    public Result findRepurchaseCoupon() {
        return couponService.findRepurchaseCoupon();
    }


    @PostMapping("/pageList")
    public Result pageList(@RequestBody CouponPageDTO couponPageDTO) {
        return couponService.pageList(couponPageDTO);
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id) {
        return couponService.findById(id);
    }

    @PostMapping("/create")
    public Result create(@RequestBody CouponCreateDTO couponCreateDTO) {
        return couponService.create(couponCreateDTO);
    }

    @PostMapping("/update")
    public Result modify(@RequestBody CouponUpdateDTO couponUpdateDTO) {
        return couponService.modify(couponUpdateDTO);
    }

    @PostMapping("/disableOrOpen")
    public Result disableOrOpen(@RequestBody CouponUpdateDTO couponUpdateDTO) {
        return couponService.disableOrOpen(couponUpdateDTO);
    }

    @PostMapping("/repurchase-disableOrOpen")
    public Result repurchaseDisableOrOpen(@RequestBody CouponUpdateDTO couponUpdateDTO) {
        LambdaUpdateWrapper<CouponDO> uw = Wrappers.lambdaUpdate(CouponDO.class)
                .set(CouponDO::getIsRelease, couponUpdateDTO.getIsRelease())
                .eq(CouponDO::getId, couponUpdateDTO.getId());
        return couponService.update(uw) ? Result.success() : Result.fail();
    }

    @PostMapping("/coupon-history/sendRepurchase")
    public Result sendRepurchase(@RequestBody SendRepurchaseDTO sendRepurchaseDTO) {
        log.info("发放优惠卷(复购卷):{}", JSON.toJSONString(sendRepurchaseDTO));
        return couponService.sendRepurchase(sendRepurchaseDTO);
    }

    @GetMapping("/coupon-history/deleteRepurchase/{userId}")
    public Result deleteRepurchase(@PathVariable Long userId) {
        return couponService.deleteRepurchase(userId);
    }

    /**
     * 根据ids查询绑定的复购卷
     * @param
     * @return
     */
    @GetMapping("/repurchase-coupon-productId/{id}")
    public Result findRepurchaseCouponByProductId(@PathVariable Long id){
        return couponService.findRepurchaseCouponByProductId(id);
    }

    @PostMapping("/findRepurchaseByProductIds")
    public Result<Map<Long,Boolean>> findRepurchaseByProductIds(@RequestBody List<Long> ids){
        return couponService.findRepurchaseByProductIds(ids);
    }

    @PostMapping("/findByCouponIds")
    public Result<List<CouponVO>> findByCouponIds(@RequestBody List<Long> couponIds){
        List<CouponDO> coupon = couponService.listByIds(couponIds);
        List<CouponVO> collect = coupon.stream().map(c -> BeanUtils.copyProperties(c, CouponVO.class)).collect(Collectors.toList());
        return Result.success(collect);
    }
}
